﻿using AuthCenter.API.Application.Command.Account;
using AuthCenter.API.DTOs;
using AuthCenter.Domain;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using AutoMapper;
using MD5Hash;
using MediatR;
using System.Diagnostics.Metrics;

namespace AuthCenter.API.Application.Handler.Account
{
    public class GetAccountHandler : IRequestHandler<GetAccountCommand, APIPaging<AccountDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<AccountRoleModel> accountRoleRepo;
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IBaseRepository<ProvincesModel> provincesRepo;
        private readonly IBaseRepository<CityModel> cityRepo;
        private readonly IBaseRepository<XianModel> xianRepo;
        private readonly IBaseRepository<LogModel> logRepo;
        private readonly IMapper mapper;

        public GetAccountHandler(IBaseRepository<AccountModel> accountRepo,
                                 IBaseRepository<AccountRoleModel> accountRoleRepo,
                                 IBaseRepository<RoleModel> roleRepo,
                                 IBaseRepository<ProvincesModel> provincesRepo,
                                 IBaseRepository<CityModel> cityRepo,
                                 IBaseRepository<XianModel> xianRepo,
                                 IBaseRepository<LogModel> logRepo,
                                 IMapper mapper)
        {
            this.accountRepo = accountRepo;
            this.accountRoleRepo = accountRoleRepo;
            this.roleRepo = roleRepo;
            this.provincesRepo = provincesRepo;
            this.cityRepo = cityRepo;
            this.xianRepo = xianRepo;
            this.logRepo = logRepo;
            this.mapper = mapper;
        }
        public Task<APIPaging<AccountDTO>> Handle(GetAccountCommand request, CancellationToken cancellationToken)
        {
            APIPaging<AccountDTO> result = new APIPaging<AccountDTO>();
            var account = accountRepo.GetAll().Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(request.Name))
            {
                account = account.Where(x=>x.Name.Contains(request.Name));
            }
            var provinces = provincesRepo.GetAll().Where(x => x.IsDel == false);
            var city = cityRepo.GetAll().Where(x => x.IsDel == false);
            var xian = xianRepo.GetAll().Where(x => x.IsDel == false);
 
            var res = mapper.Map<List<AccountDTO>>(account);
            foreach (var item in res)
            {
                item.ProvincesName = provincesRepo.GetAll().FirstOrDefault(x => x.Id == item.Provinces).ProvincesName;
                item.CityName = cityRepo.GetAll().FirstOrDefault(x => x.Id == item.City).CityName;
                item.XianName = xianRepo.GetAll().FirstOrDefault(x => x.Id == item.Xian).XianName;

                var arQuery = accountRoleRepo.GetAll().Where(x => !x.IsDel && x.AccountId == item.Id);
                var rQuery = roleRepo.GetAll().Where(x => !x.IsDel);
                var accountinfo = (from a in arQuery
                                   join b in rQuery on a.RoleId equals b.Id
                                   select new {a,b}).ToList();
                List<string> roleNameList = accountinfo.Select(x=>x.b.RoleName).ToList();
                item.RoleName = string.Join(",", roleNameList);
            }
            result.TotalCount = res.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            result.PageDatas = res.OrderByDescending(x => x.CreateDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(); ;
            return Task.FromResult(result);
        }
    }
}
